import requests
from playwright.sync_api import Playwright, sync_playwright
import os
import time
import base64
from rpa_api import RpaAPI


def get_img(data: bytes) -> str:
    """
    base64读图片
    :param data: 字节流图片
    :return: encodestr的图片
    """
    try:
        encodestr = str(base64.b64encode(data), 'utf-8')
    except TypeError:
        encodestr = base64.b64encode(data)
    return encodestr


def get_cookies():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False, args=['--start-maximized'],
                                             executable_path=r"D:\HFTRPA\chrome\chrome.exe")
        chrome = browser.new_context(no_viewport=True)
        page = chrome.new_page()
        page.goto('https://hz.hanfatong.com/stargate/#/login')
        page.locator('//div[text()="密码登录"]').click()
        page.get_by_placeholder('账号').fill('郭承志')
        page.get_by_placeholder('密码').fill('1Ku879^u')
        # 点击验证码
        if os.path.exists(os.path.join(os.getcwd(), 'code.png')):
            os.remove(os.path.join(os.getcwd(), 'code.png'))
            time.sleep(1)
        page.locator('//div[@class="login-code"]/img').screenshot(path=os.path.join(os.getcwd(), 'code.png'))
        time.sleep(1)
        with open(os.path.join(os.getcwd(), 'code.png'), 'rb') as f:
            code = RpaAPI.verification_ddddocr(get_img(f.read()))
        page.get_by_placeholder("验证码").fill(code['data'])
        page.wait_for_load_state()
        page.wait_for_selector('//h1[text()="汉法通RPA智能调度系统"]')
        chrome.storage_state(path="state.json")
        time.sleep(1)
        chrome.storage_state(path="state.json")


cookies = {
    'HZ-Stargate-Token': 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxNzksInVzZXJfa2V5IjoiNjM5Y2FkZDUtMjdlZi00NzY3LTgxMmItYTE4OTNjYjYwYjAxIiwidXNlcm5hbWUiOiJndW9jaGVuZ3poaSJ9.-VG6JHREZQqY8dKwKZtrRkK8KBPOrz_CzLSxRt2t6g1r6lNRKGejzJAuVBUS0FsERv7Dv-mcncM8Gyg_PHsKmA',
}

headers = {
    # 'accept': 'application/json, text/plain, */*',
    # 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    # 'appcode': 'rpa-dispatch',
    'authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxNzksInVzZXJfa2V5IjoiNjM5Y2FkZDUtMjdlZi00NzY3LTgxMmItYTE4OTNjYjYwYjAxIiwidXNlcm5hbWUiOiJndW9jaGVuZ3poaSJ9.-VG6JHREZQqY8dKwKZtrRkK8KBPOrz_CzLSxRt2t6g1r6lNRKGejzJAuVBUS0FsERv7Dv-mcncM8Gyg_PHsKmA',
    # 'content-type': 'application/json;charset=UTF-8',
    # # 'cookie': 'HZ-Stargate-Token=eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxNzksInVzZXJfa2V5IjoiNjM5Y2FkZDUtMjdlZi00NzY3LTgxMmItYTE4OTNjYjYwYjAxIiwidXNlcm5hbWUiOiJndW9jaGVuZ3poaSJ9.-VG6JHREZQqY8dKwKZtrRkK8KBPOrz_CzLSxRt2t6g1r6lNRKGejzJAuVBUS0FsERv7Dv-mcncM8Gyg_PHsKmA',
    # 'origin': 'https://hz.hanfatong.com',
    # 'priority': 'u=1, i',
    # 'referer': 'https://hz.hanfatong.com/hyperion/',
    # 'sec-ch-ua': '"Chromium";v="130", "Microsoft Edge";v="130", "Not?A_Brand";v="99"',
    # 'sec-ch-ua-mobile': '?0',
    # 'sec-ch-ua-platform': '"Windows"',
    # 'sec-fetch-dest': 'empty',
    # 'sec-fetch-mode': 'cors',
    # 'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
}

params = {
    'pageSize': '1000',
    'pageNum': '1',
}
# get_cookies()
json_data = {}

response = requests.post(
    'https://hz.hanfatong.com/rdc-server/rpa-program/list',
    params=params,
    # cookies=cookies,
    headers=headers,
    json=json_data,
)
print(response.json())

